/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultarClienteController.java
 *
 * Created on 06/07/2009, 15:11:14
 */
package com.celiosilva.simbanc.controller.cliente;

import com.celiosilva.simbanc.beans.Cliente;
import com.celiosilva.simbanc.business.ClienteBusiness;
import com.celiosilva.simbanc.controller.util.Conversor;
import com.celiosilva.simbanc.controller.util.OrdenarClienteCodigo;
import com.celiosilva.simbanc.controller.util.OrdenarClienteSobrenome;
import com.celiosilva.simbanc.exceptions.AcessoNegadoException;
import com.celiosilva.simbanc.util.Sorteador;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import org.jdesktop.beansbinding.Binding;

/**
 *
 * @author Administrador
 */
public class ConsultarClienteController extends javax.swing.JInternalFrame{

    
    /** Creates new form ConsultarClienteController */
    public ConsultarClienteController() {
        initComponents();
    }

    private void mostrarClientes(List<Cliente> clientes){
        Binding b = bindingGroup.getBinding("clientes");
        b.unbind();
        this.clientes.clear();
        this.clientes.addAll(clientes);
        b.bind();
        tblClientes.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        clientes = new LinkedList<Cliente>();
        clienteSelecionado = new com.celiosilva.simbanc.beans.ClienteFisico();
        btgSorteioClientes = new javax.swing.ButtonGroup();
        btgOrdenacaoClientes = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblClientes = new javax.swing.JTable();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        txtCodigo = new javax.swing.JTextField();
        btnConsultarCodigo = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        txtNome = new javax.swing.JTextField();
        btnConsultarNome = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        btnConsultarCPF = new javax.swing.JButton();
        txtCPF = new javax.swing.JFormattedTextField();
        jPanel4 = new javax.swing.JPanel();
        btnConsultarCNPJ = new javax.swing.JButton();
        txtCNPJ = new javax.swing.JFormattedTextField();
        jPanel5 = new javax.swing.JPanel();
        txtMaximoContemplados = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnSortearClientes = new javax.swing.JButton();
        rdbFisico = new javax.swing.JRadioButton();
        rdbJuridico = new javax.swing.JRadioButton();
        rdbTodos = new javax.swing.JRadioButton();
        jPanel6 = new javax.swing.JPanel();
        rdbOrdCodigo = new javax.swing.JRadioButton();
        rdbOrdNome = new javax.swing.JRadioButton();
        rdbOrdSobrenome = new javax.swing.JRadioButton();
        jSeparator1 = new javax.swing.JSeparator();
        btnAlterar = new javax.swing.JButton();
        btnVisualizar = new javax.swing.JButton();
        btnBloquear = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnReativar = new javax.swing.JButton();

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tblClientes, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codigo}"), clienteSelecionado, org.jdesktop.beansbinding.BeanProperty.create("codigo"));
        bindingGroup.addBinding(binding);

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Consultar Clientes por:");

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, clientes, tblClientes, "clientes");
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codigo}"));
        columnBinding.setColumnName("Codigo");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nome}"));
        columnBinding.setColumnName("Nome");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${sobrenome}"));
        columnBinding.setColumnName("Sobrenome");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cidade}"));
        columnBinding.setColumnName("Cidade");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${estadoCliente}"));
        columnBinding.setColumnName("Estado Cliente");
        columnBinding.setColumnClass(com.celiosilva.simbanc.beans.enums.EstadoCliente.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(tblClientes);

        btnConsultarCodigo.setText("Consultar");
        btnConsultarCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarCodigoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 333, Short.MAX_VALUE)
                .addComponent(btnConsultarCodigo)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConsultarCodigo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Código", jPanel1);

        btnConsultarNome.setText("Consultar");
        btnConsultarNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarNomeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 167, Short.MAX_VALUE)
                .addComponent(btnConsultarNome)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConsultarNome))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Nome", jPanel2);

        btnConsultarCPF.setText("Consultar");
        btnConsultarCPF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarCPFActionPerformed(evt);
            }
        });

        try {
            txtCPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 215, Short.MAX_VALUE)
                .addComponent(btnConsultarCPF)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnConsultarCPF)
                    .addComponent(txtCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("CPF", jPanel3);

        btnConsultarCNPJ.setText("Consultar");
        btnConsultarCNPJ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarCNPJActionPerformed(evt);
            }
        });

        try {
            txtCNPJ.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 215, Short.MAX_VALUE)
                .addComponent(btnConsultarCNPJ)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnConsultarCNPJ)
                    .addComponent(txtCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("CNPJ", jPanel4);

        txtMaximoContemplados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMaximoContempladosActionPerformed(evt);
            }
        });

        jLabel1.setText("Máximo Contemplados:");

        btnSortearClientes.setText("Sortear");
        btnSortearClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSortearClientesActionPerformed(evt);
            }
        });

        btgSorteioClientes.add(rdbFisico);
        rdbFisico.setText("Físico");

        btgSorteioClientes.add(rdbJuridico);
        rdbJuridico.setText("Jurídico");

        btgSorteioClientes.add(rdbTodos);
        rdbTodos.setSelected(true);
        rdbTodos.setText("Todos");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtMaximoContemplados, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rdbFisico)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdbJuridico)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdbTodos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 122, Short.MAX_VALUE)
                .addComponent(btnSortearClientes)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtMaximoContemplados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSortearClientes)
                    .addComponent(rdbFisico)
                    .addComponent(rdbJuridico)
                    .addComponent(rdbTodos))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Sorteio Clientes", jPanel5);

        btgOrdenacaoClientes.add(rdbOrdCodigo);
        rdbOrdCodigo.setText("Código");
        rdbOrdCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbOrdCodigoActionPerformed(evt);
            }
        });

        btgOrdenacaoClientes.add(rdbOrdNome);
        rdbOrdNome.setText("Nome");
        rdbOrdNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbOrdNomeActionPerformed(evt);
            }
        });

        btgOrdenacaoClientes.add(rdbOrdSobrenome);
        rdbOrdSobrenome.setText("Sobrenome/Nome Fantasia");
        rdbOrdSobrenome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbOrdSobrenomeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rdbOrdCodigo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdbOrdNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdbOrdSobrenome)
                .addContainerGap(263, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rdbOrdCodigo)
                    .addComponent(rdbOrdNome)
                    .addComponent(rdbOrdSobrenome))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Ordenar Clientes", jPanel6);

        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnVisualizar.setText("Visualizar");
        btnVisualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarActionPerformed(evt);
            }
        });

        btnBloquear.setText("Bloquear");
        btnBloquear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBloquearActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnReativar.setText("Reativar");
        btnReativar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReativarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnAlterar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnVisualizar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBloquear)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnReativar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAlterar)
                    .addComponent(btnVisualizar)
                    .addComponent(btnBloquear)
                    .addComponent(btnCancelar)
                    .addComponent(btnReativar))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConsultarCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarCodigoActionPerformed
        Cliente cliente = ClienteBusiness.getInstance().consultarClientePorCodigo((Integer)Conversor.STRING_INTEGER.converterPara(this.txtCodigo.getText()));
        this.mostrarClientes(Arrays.asList(cliente));
    }//GEN-LAST:event_btnConsultarCodigoActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        try {
            this.mostrarJanelaInterna(new AlterarClienteController(clienteSelecionado.getCodigo()));
        } catch (AcessoNegadoException ex) {
            this.mostrarAcessoNegado(ex);
        }
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnVisualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarActionPerformed
        try {
            this.mostrarJanelaInterna(new VisualizarClienteController(clienteSelecionado.getCodigo()));
        } catch (AcessoNegadoException ex) {
            this.mostrarAcessoNegado(ex);
        }
    }//GEN-LAST:event_btnVisualizarActionPerformed

    private void btnBloquearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBloquearActionPerformed
        try {
            this.mostrarJanelaInterna(new BloquearClienteController(clienteSelecionado.getCodigo()));
        } catch (AcessoNegadoException ex) {
            this.mostrarAcessoNegado(ex);
        }
    }//GEN-LAST:event_btnBloquearActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        try {
            this.mostrarJanelaInterna(new CancelarClienteController(clienteSelecionado.getCodigo()));
        } catch (AcessoNegadoException ex) {
            this.mostrarAcessoNegado(ex);
        }
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnReativarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReativarActionPerformed
        try {
            this.mostrarJanelaInterna(new ReativarClienteController(clienteSelecionado.getCodigo()));
        } catch (AcessoNegadoException ex) {
            this.mostrarAcessoNegado(ex);
        }
    }//GEN-LAST:event_btnReativarActionPerformed

    private void btnConsultarNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarNomeActionPerformed
        this.mostrarClientes(ClienteBusiness.getInstance().consultarPorNome(this.txtNome.getText()));
    }//GEN-LAST:event_btnConsultarNomeActionPerformed

    private void btnConsultarCPFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarCPFActionPerformed
        Cliente cliente = ClienteBusiness.getInstance().consultarPorCPF(this.txtCPF.getText());
        this.mostrarClientes(Arrays.asList(cliente));
    }//GEN-LAST:event_btnConsultarCPFActionPerformed

    private void btnConsultarCNPJActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarCNPJActionPerformed
        Cliente cliente = ClienteBusiness.getInstance().consultarPorCNPJ(this.txtCNPJ.getText());
        this.mostrarClientes(Arrays.asList(cliente));
    }//GEN-LAST:event_btnConsultarCNPJActionPerformed

    private void txtMaximoContempladosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMaximoContempladosActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtMaximoContempladosActionPerformed

    private void btnSortearClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSortearClientesActionPerformed
        Sorteador<Cliente> sorteador = new Sorteador<Cliente>();
        if (rdbFisico.isSelected()){
            sorteador.setParticipantes(ClienteBusiness.getInstance().listarClientesFisicos());
        } else if (rdbJuridico.isSelected()){
            sorteador.setParticipantes(ClienteBusiness.getInstance().listarClientesJuridicos());
        } else {
            sorteador.setParticipantes(ClienteBusiness.getInstance().listarClientes());
        }
        List<Cliente> contemplados = sorteador.sortear((Integer)Conversor.STRING_INTEGER.converterPara(this.txtMaximoContemplados.getText()));
        this.mostrarClientes(contemplados);
    }//GEN-LAST:event_btnSortearClientesActionPerformed

    private void rdbOrdCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbOrdCodigoActionPerformed
        List<Cliente> aux = new ArrayList<Cliente>(this.clientes);
        OrdenarClienteCodigo ordenador = new OrdenarClienteCodigo();
        Collections.sort(aux, ordenador);
        this.mostrarClientes(aux);
    }//GEN-LAST:event_rdbOrdCodigoActionPerformed

    private void rdbOrdNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbOrdNomeActionPerformed
        List<Cliente> aux = new ArrayList<Cliente>(this.clientes);
        Collections.sort(aux);
        this.mostrarClientes(aux);
    }//GEN-LAST:event_rdbOrdNomeActionPerformed

    private void rdbOrdSobrenomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbOrdSobrenomeActionPerformed
        List<Cliente> aux = new ArrayList<Cliente>(this.clientes);
        OrdenarClienteSobrenome ordenador = new OrdenarClienteSobrenome();
        Collections.sort(aux, ordenador);
        this.mostrarClientes(aux);
    }//GEN-LAST:event_rdbOrdSobrenomeActionPerformed


    private void mostrarJanelaInterna(JInternalFrame janela){
        this.getParent().add(janela);
        janela.setVisible(true);
    }

    private void mostrarAcessoNegado(AcessoNegadoException ex){
        JOptionPane.showMessageDialog(this, ex.getMessage(), "Acesso Negado", JOptionPane.ERROR_MESSAGE);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btgOrdenacaoClientes;
    private javax.swing.ButtonGroup btgSorteioClientes;
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnBloquear;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConsultarCNPJ;
    private javax.swing.JButton btnConsultarCPF;
    private javax.swing.JButton btnConsultarCodigo;
    private javax.swing.JButton btnConsultarNome;
    private javax.swing.JButton btnReativar;
    private javax.swing.JButton btnSortearClientes;
    private javax.swing.JButton btnVisualizar;
    private com.celiosilva.simbanc.beans.ClienteFisico clienteSelecionado;
    private java.util.List<Cliente> clientes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JRadioButton rdbFisico;
    private javax.swing.JRadioButton rdbJuridico;
    private javax.swing.JRadioButton rdbOrdCodigo;
    private javax.swing.JRadioButton rdbOrdNome;
    private javax.swing.JRadioButton rdbOrdSobrenome;
    private javax.swing.JRadioButton rdbTodos;
    private javax.swing.JTable tblClientes;
    private javax.swing.JFormattedTextField txtCNPJ;
    private javax.swing.JFormattedTextField txtCPF;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtMaximoContemplados;
    private javax.swing.JTextField txtNome;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
